package com.tgh.gles2.c3.blend;

import com.tgh.utils.GLRect2D;
import com.tgh.utils.GLShaderManager;
import com.tgh.utils.GLShaderManager.ShaderType;

import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MySurfaceView extends GLSurfaceView {

    public MySurfaceView(Context context) {
        super(context);
        init();
    }

    public MySurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x=event.getX();
        float y=event.getY();
        switch (event.getAction()&MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_UP:
                lastX=x;
                lastY=y;
                return true;
            case MotionEvent.ACTION_MOVE:
                float dy=lastY-y;
                if (dy>10||dy<-10) {
                    float sign = Math.signum(dy);
                    rectY+=sign*0.025f;
                }
                float dx=x-lastX;
                if (dx>10 || dx<-10) {
                    float sign = Math.signum(dx);
                    rectX+=sign*0.025f;
                }
                requestRender();
                lastX=x;
                lastY=y;
                return true;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }
    
    private void init() {
        setEGLContextClientVersion(2);
        setRenderer(new SceneRenderer());
        setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
    
    private float rectX,rectY;
    private float lastX,lastY;
    
    private class SceneRenderer implements Renderer{

        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            GLES20.glClearColor(1, 1, 1, 1);
            shaderManager = new GLShaderManager();
            shaderManager.initStockShader();
            fixRect = new GLRect2D(0,0,0.5f,0.5f);
            moveRect = new GLRect2D(0, 0, 0.2f, 0.2f);
        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            GLES20.glViewport(0, 0, width, height);
        }

        @Override
        public void onDrawFrame(GL10 gl) {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
            drawBlackRect();
            drawRedRect();
            drawGreenRect();
            drawBlueRect();
            drawMoveRect();
        }

        private void drawBlackRect() {
            fixRect.moveTo(0.25f, -0.75f);
            float[] black={0,0,0,1};
            shaderManager.useStockShader(ShaderType.Identity, black);
            fixRect.draw();
        }
        
        private void drawRedRect() {
            fixRect.moveTo(-0.75f, 0.25f);
            float[] red={1,0,0,0.5f};
            shaderManager.useStockShader(ShaderType.Identity, red);
            fixRect.draw();
        }
        
        private void drawGreenRect() {
            fixRect.moveTo(0.25f, 0.25f);
            float[] green={0,1,0,1};
            shaderManager.useStockShader(ShaderType.Identity, green);
            fixRect.draw();
        }
        
        private void drawBlueRect() {
            fixRect.moveTo(-0.75f, -0.75f);
            float[] blue={0,0,1,1};
            shaderManager.useStockShader(ShaderType.Identity, blue);
            fixRect.draw();
        }
        
        private void drawMoveRect() {
            GLES20.glEnable(GLES20.GL_BLEND);
            GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
            float[] red={1,0,0,0.5f};
            shaderManager.useStockShader(ShaderType.Identity, red);
            moveRect.moveTo(rectX, rectY);
            moveRect.draw();
            GLES20.glDisable(GLES20.GL_BLEND);
        }
        
        private GLShaderManager shaderManager;
        private GLRect2D fixRect;
        private GLRect2D moveRect;
    }
}
